package com.cj.mapper;

import com.cj.pojo.Comment;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface CommentMapper {
        @Insert("INSERT INTO comment(content, article_id, user_id, parent_id, status, like_count, is_deleted, created_at, updated_at) " +
                "VALUES(#{content}, #{articleId}, #{userId}, #{parentId}, #{status}, #{likeCount}, #{isDeleted}, #{createdAt}, #{updatedAt})")
        void add(Comment comment);

        @Update("UPDATE comment SET content=#{content}, status=#{status}, like_count=#{likeCount}, " +
                "is_deleted=#{isDeleted}, updated_at=#{updatedAt} WHERE id=#{id}")
        void update(Comment comment);

        @Delete("DELETE FROM comment WHERE id=#{id}")
        void delete(Long id);

        @Select("SELECT * FROM comment WHERE id=#{id}")
        Comment findById(Long id);

        List<Comment> findByArticleId(Long articleId, Integer pageNum, Integer pageSize);

        List<Comment> findByUserId(Long userId, Integer pageNum, Integer pageSize);

        List<Comment> findReplies(Long parentId, Integer pageNum, Integer pageSize);

        @Update("UPDATE comment SET like_count=like_count+1 WHERE id=#{id}")
        void incrementLikeCount(Long id);

        void batchDelete(List<Long> ids);

        @Select("SELECT SUM(like_count) FROM comment")
        Integer countTotalLikes();

        @Select("SELECT SUM(like_count) FROM comment WHERE created_at BETWEEN #{startTime} AND #{endTime}")
        Integer countLikesBetween(@Param("startTime") LocalDateTime startTime, 
                                @Param("endTime") LocalDateTime endTime);

        @Select("SELECT id, content, like_count FROM comment ORDER BY like_count DESC LIMIT #{limit}")
        List<Map<String, Object>> getTopLikedComments(@Param("limit") int limit);
}
